<?php echo form_open('lignes/miseajour');?>
<div class="divrondform">
	<div style="margin-left:50px; margin-top:20px;">
		<table>
			<tr>
				<td>
					<table id="lignes_commande_grid" style="cursor:pointer"></table>
				</td>
			</tr>
			<tr>
				<td id="total">
				</td>
			</tr>
			<tr>
				<td valign="top" width="300px" style="margin:auto;">
					<input class="boutons" name="BLignes" type="submit" value="Valider" style="width:130 px" />
					<input class="boutons" name="BLignes" type="submit" value="Retour" style="width:130 px" />
					<input type="hidden" name="NLigne" id="NLigne" value="NULL"/>
					<input type="hidden" id="CATEG" value="NON">
				</td>	
			</tr>
		</table>
	</div>
	<div id="pager"></div>
	<?php echo $LignesCommandeGrille; ?>
	<br>
</div>    
</form>
<script>

function dblClick(id) {
	$("#NLigne").val(id);
	$("#BCM").click();
}
function selectRow(id) {
    $("#NLigne").val(id);	
}

function CalculMontant(rowid,name,val,iRow,iCol) {
	var grille = $("#lignes_commande_grid"),
		pu     = parseFloat(grille.jqGrid("getCell", rowid, iCol + 1));
		m      = 0;

	grille.jqGrid("setRowData", rowid, {montant:parseFloat(val) * pu});

	afterLoading();

	var griddata = grille.jqGrid('getGridParam', 'data');
	var g = JSON.stringify(griddata);
	$("#NLigne").val(g);
}

function afterLoading() {
	var grille = $("#lignes_commande_grid");
	var index   = grille.jqGrid('getGridParam', '_index');
	var n, i, c;
	var c_courant = '0';
	var clib;
	var m = 0; // Montant du total de la commande du user
	var CATEG = $("#CATEG"); 

	for(i in index) {
		// Ajout "gras" et "coloration" et insertion des catégories
		n = parseFloat(grille.jqGrid('getCell', i, 'nombre' ));
		c = grille.jqGrid('getCell', i, 'categorieId');
		if(c != c_courant && CATEG.val() == "NON") {
			clib = grille.jqGrid('getCell', i, 'catlibelle');
			grille.jqGrid('addRowData', 'CAT'+c, {code:'',libelle:clib,poids:'',nombre:'',pu:'',montant:''}, 'before', i);
			grille.jqGrid('setCell', 'CAT'+c,'nombre','', {'color':'#DDDDDD'});
			grille.jqGrid('setCell', 'CAT'+c,'pu','', {'color':'#DDDDDD'});
			grille.jqGrid('setCell', 'CAT'+c,'montant','', {'color':'#DDDDDD'});
			grille.jqGrid('setCell', 'CAT'+c,'libelle','', {'font-weight':'bold'});
			grille.jqGrid('setCell', 'CAT'+c,'libelle','', {'color':'red'});
			grille.jqGrid('setCell', 'CAT'+c,'libelle','', {'background-color':'#DDDDDD'});
			grille.jqGrid('setCell', 'CAT'+c,'code','', {'background-color':'#DDDDDD'});
			grille.jqGrid('setCell', 'CAT'+c,'poids','', {'background-color':'#DDDDDD'});
			grille.jqGrid('setCell', 'CAT'+c,'nombre','', {'background-color':'#DDDDDD'});
			grille.jqGrid('setCell', 'CAT'+c,'pu','', {'background-color':'#DDDDDD'});
			grille.jqGrid('setCell', 'CAT'+c,'montant','', {'background-color':'#DDDDDD'});
			c_courant = c;
		}
		grille.jqGrid('setCell', i, 'nombre', '', {'font-weight': 'bold'});
		if(n > 0) { 
			grille.jqGrid('setCell', i, 'nombre', '', {'color': 'green'});
			grille.jqGrid('setCell', i, 'montant', '', {'font-weight': 'bold'});
		}
		// Calcul montant total
		m = m + parseFloat(grille.jqGrid('getCell', i, 'montant' ));
	}
	$("#total").html("<span style='font-weight: bold;'>Montant total : " + m.toFixed(2) + " &euro;</span>");

	if(CATEG.val() == "NON") { CATEG.val("OUI"); }
}

</script>
	

